

local shield = nil
local lastshield = 0
local maxshield = nil
local nexthud = 0
local alpha = 0
local broken = true
local damage = false
local regenalpha = 0
local shieldbar = 0


function recieveVar( data )
	shield = data:ReadFloat()
	maxshield = data:ReadLong()
end

function PaintHUD()
	
	client = client or LocalPlayer()
	shield = ( shield or 0 )
	maxshield = ( maxshield or 0 )
	outlineColor = Color( 100, 100, 100, 150 )
	fracshield = shield/maxshield
	if ( shield == 0 ) then
		outlineColor = Color( math.abs(math.sin(CurTime()*10)) * 255, 0, 0, 150 )
	end
	if ( lastshield != shield ) then
		//nexthud = CurTime() + 3
		if ( lastshield > shield ) then
			damage = true
		end
		if ( lastshield < shield ) then
			damage = false
		end
		if ( lastshield < shield && lastshield == 0 ) then
			regenalpha = 100
		end
		lastshield = shield
		alpha = 700
	end
	if ( regenalpha > 0 ) then
				draw.RoundedBox(0, 0,ScrH()/2 * ( regenalpha/100), ScrW(), 
				ScrH() * ( (100-regenalpha)/100), 
				Color(0, 0, 200, regenalpha ) )
				regenalpha = regenalpha - 1
	end
	if ( alpha > 0 ) then
		if ( damage ) then
			if ( shield > 0 ) then
				draw.RoundedBox(2, 90, ScrH() - 200, 250, 75, Color(0, 0, 200, math.max(alpha*4 - 2600, 0 ) ) )
			end
			if ( shield == 0 ) then
				draw.RoundedBox(0, 0,ScrH()/2 * ( (50 - math.max(alpha - 650, 0 ))/50), ScrW(), 
				ScrH() * ( math.max(alpha - 650, 0 )/50), 
				Color(0, 0, 200, 100 ) )
			end
		end
	shieldbar = shieldbar + ((200*fracshield) - shieldbar)*0.2
	if (shieldbar > 200) then
		shieldbar = 200
	end
	if (shieldbar < 0) then
		shieldbar = 0
	end
	//draw.RoundedBox(2, 105, ScrH() - 200 - 5, maxshield * 1 + 10, 30, outlineColor)
	//draw.RoundedBox(2, 110, ScrH() - 200, shield * 1, 20, Color(0, 200, 255, math.min(alpha, 255 ) ) )
	//draw.RoundedBox(2, 110, ScrH() - 200, shield * 1, 10, Color(0, 230, 255, math.min(alpha, 255 ) ) )
	draw.RoundedBox(2, 105, ScrH() - 170 - 5, 200 + 10, 30, outlineColor)
	if ( shield > 0 ) then
		draw.RoundedBox(2, 110, ScrH() - 170, shieldbar, 20, Color(0, 200, 255, math.min(alpha, 255 ) ) )
		draw.RoundedBox(2, 110, ScrH() - 170, shieldbar, 10, Color(0, 230, 255, math.min(alpha, 255 ) ) )
	end
	draw.SimpleText( string.format("SHIELD: %i", shield), "default", 110, ScrH() - 175, 
	Color(255,255,255,255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
	alpha = alpha - 1
	else
	damage = false
	end
end

usermessage.Hook( "ShieldSys", recieveVar )
hook.Add( "HUDPaint", "ShieldSys.HUD", PaintHUD )